package com.apex.lottery.controller;

import com.apex.lottery.common.result.CommonResult;
import com.apex.lottery.common.utils.JacksonUtil;
import com.apex.lottery.controller.param.CreatePrizeParam;
import com.apex.lottery.controller.param.PageParam;
import com.apex.lottery.controller.result.FindPrizeListResult;
import com.apex.lottery.service.AwardService;
import com.apex.lottery.service.PictureService;
import com.apex.lottery.service.dto.PageListDTO;
import com.apex.lottery.service.dto.PrizeDTO;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import static com.apex.lottery.controller.converter.AwardConverter.convertToFindPrizeListResult;

@RestController
public class AwardController {

    private static final Logger logger = LoggerFactory.getLogger(AwardController.class);

    @Autowired
    private PictureService pictureService;

    @Autowired
    private AwardService awardService;

    @PostMapping("/pic/upload")
    public String uploadPicture(MultipartFile file) {
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     *  RequestPart:用于接收表单数据的 multipart/form-data
     *
     * @param param  表单数据用 @RequestPart
     * @param picFile 文件
     * @return
     */
    @PostMapping("/prize/create")
    public CommonResult<Long> createPrize(@Valid @RequestPart("param") CreatePrizeParam param, @RequestPart("prizePic") MultipartFile picFile) {
        logger.info("createPrize CreatePrizeParam:{}", JacksonUtil.writeValueAsString(param));

        return CommonResult.success(awardService.createPrize(param, picFile));
    }

    @GetMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param) {
        logger.info("findPrizeList PageParam:{}", JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = awardService.findPrizeList(param);

        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));
    }

}
